package com.common.convert;

import com.common.convert.param.ConvertInput;
import com.common.convert.param.ConvertResult;
import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
import org.jodconverter.core.document.DocumentFormat;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

public interface Converter {

    ConvertResult convert(ConvertInput convertInput, String outType);

    default DocumentFormat getFormat(String fileType) {
        return DefaultDocumentFormatRegistry.getFormatByExtension(fileType);
    }

    default String getType(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    Set<FormatSupport> supports();

    default Set<String> supportByType(String inputType) {
        return supportMap().get(inputType);
    }

    default Map<String, Set<String>> supportMap() {
        Map<String, Set<String>> supportMap = new LinkedHashMap<>();
        for (FormatSupport formatSupport : supports()) {
            Set<String> list = supportMap.getOrDefault(formatSupport.getFromFormat(), new LinkedHashSet<String>());
            list.add(formatSupport.getToFormat());
            supportMap.put(formatSupport.getFromFormat(), list);
        }
        return supportMap;
    }
}
